package com.share.finance.collection.core.schedule;

import com.share.common.utils.DateUtil;
import com.share.config.redisson.RedisLock;
import com.share.finance.collection.core.service.IOverdueInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * 拉取逾期数据并分析
 */
@Slf4j
@Component
public class PullOverBillSchedule {
    @Autowired
    private RedisLock redisLock;

    @Autowired
    private IOverdueInfoService overdueInfoService;


    /**
     * 逾期用户数据拉取 与提炼
     * 今天分析昨天的数据
     */
    @Scheduled(cron = "10 18 2 * * ?")
    public void pullOverdueData() {
        final String LOCK_KEY = "collection:pullOverdueData";
        boolean locked = false;

        try {
            locked = redisLock.tryLock(LOCK_KEY, 3, TimeUnit.MINUTES);
            if (locked) {
                log.info("模拟放款 + 生成还款计划");
                overdueInfoService.pullOverdueData(null);
            }
        } catch (Exception e) {
            log.error("模拟放款 + 生成还款计划 异常 ", e);
        } finally {
            if (locked) {
                redisLock.unlock(LOCK_KEY);
            }
        }
    }
}
